Find the first repeated character with smallest first occurrence indexΒΆ
Find the first repeated character of a given string where the index of first occurrence is smallest.
def first_repeated_char_smallest_distance(S):
temp = {}
for c in S:
if c in temp:
return c;
else:
temp[c] = 0
return 'None'
# test
print(first_repeated_char_smallest_distance("abcabc")) # a
print(first_repeated_char_smallest_distance("abbcabc")) # b
print(first_repeated_char_smallest_distance("abcbabc")) # b
print(first_repeated_char_smallest_distance("abcxxy")) # x
print(first_repeated_char_smallest_distance("abc")) # None